package com.luoxiang.socialandroid.fragment;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.HomepageAdapter;
import com.luoxiang.socialandroid.viewpager.CustomViewPager;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 社区的fragment
 * @systemUser Administrator
 * @Author LiangXin
 * @Date 08-20-2022 周六 10:56
 */
public class CommunityFragment extends Fragment {
    private TabLayout tab_home;
    private List<Fragment> mFragments;
    private ArrayList<String> list = new ArrayList<>();
    private ArrayList<Fragment> fragmentlist = new ArrayList<>();
    private ViewPager2 vp_community;
    private RadioButton rb_tuijian, rb_fujin;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_community, container, false);
        initView();
        return view;
    }

    private void initView() {
        tab_home = view.findViewById(R.id.tab_home);
        vp_community = view.findViewById(R.id.vp_community);
        UltimateBarX.addStatusBarTopPadding(view.findViewById(R.id.lin_home));
        vp_community.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        list.clear();
        fragmentlist.clear();
        list.add(getString(R.string.mi_tuijian));
        list.add(getString(R.string.mi_fujin));

        fragmentlist.add(new CommunityFirstFragment());
        fragmentlist.add(new CommunitySecondFragment());

        vp_community.setAdapter(new FragmentStateAdapter(getChildFragmentManager(), getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragmentlist.get(position);
            }

            @Override
            public int getItemCount() {
                return fragmentlist.size();
            }
        });
        new TabLayoutMediator(tab_home, vp_community, true, (tab, position) -> {
            tab.setText(list.get(position));
        }).attach();
        setTabStyle();

        tab_home.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                TextView tv = customView.findViewById(R.id.tab_tv);
                tv.setTextSize(20);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                TextView tv = customView.findViewById(R.id.tab_tv);
                tv.setTextSize(16);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        tab_home.bringToFront();
    }

    @SuppressLint({"WrongConstant", "ResourceType"})
    private void setTabStyle() {
        //根据Tab数量循环来设置
        for(int i = 0;i < list.size();i++){
            TabLayout.Tab tab = this.tab_home.getTabAt(i);
            if(tab != null) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.text_tablayout, null);
                //设置Tab标题
                ((TextView) view.findViewById(R.id.tab_tv)).setText(list.get(i));
                //第一个默认为选择样式
                if(i == 0) {
                    ((TextView) view.findViewById(R.id.tab_tv)).setTextSize(20);
                    ((TextView) view.findViewById(R.id.tab_tv)).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                }else {
                }
                //最后添加view到Tab上面
                tab.setCustomView(view);
            }
        }
    }
}
